package com.zwd.deep.service;

import com.alibaba.fastjson2.JSONObject;
import com.zwd.deep.utils.OkHttpUtil;
import jakarta.annotation.PostConstruct;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

/**
 * @author zwd
 */
@Service
public class DeepService {


    @Value("${deep.url}")
    private String url;

    @Resource
    private OkHttpUtil okHttpUtil;


    public String getTaskId(MultipartFile image) {
        String result;
        try {
            String urlAndPath = url + "/predict";
            String json = okHttpUtil.postWithFormParams(urlAndPath, Map.of("file", image));
            JSONObject jsonObject = JSONObject.parseObject(json);
            result = jsonObject.get("description").toString();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return result;
    }

    public String getResult(MultipartFile image) {
        String result;
        try {
            String urlAndPath = url + "/predict";
            String json = okHttpUtil.postWithFormParams(urlAndPath, Map.of("file", image));
            JSONObject jsonObject = JSONObject.parseObject(json);
            result = jsonObject.get("describe").toString();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return result;
    }

}
